using System.Linq;
using Engine;
using Engine.Graphics;
using Game;

namespace RuthlessConquest {
    public class GameResultDialog : Dialog {
        public LabelWidget LargeLabelWidget;
        public LabelWidget SmallLabelWidget;
        public BevelledButtonWidget Button1Widget;
        public BevelledButtonWidget Button2Widget;
        public const string fName = "RCGameResultDialog";

        public GameResultDialog(HumanPlayer player) {
            HorizontalAlignment = WidgetAlignment.Center;
            VerticalAlignment = WidgetAlignment.Center;
            Size = new Vector2(500f, 250f);
            Widget[] obj = [
                new RectangleWidget { FillColor = Colors.Back, OutlineColor = Colors.ForeDim, OutlineThickness = 2f }, new InterlaceWidget(), null
            ];
            StackPanelWidget panel1 = new() {
                Direction = LayoutDirection.Vertical,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Near,
                Margin = new Vector2(10f, 10f)
            };
            LargeLabelWidget = new LabelWidget { Margin = new Vector2(0f, 5f), HorizontalAlignment = WidgetAlignment.Center };
            panel1.Children.Add(LargeLabelWidget);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            SmallLabelWidget = new LabelWidget {
                FontScale = 0.75f,
                Color = Colors.ForeDim,
                TextAnchor = TextAnchor.HorizontalCenter,
                WordWrap = true,
                HorizontalAlignment = WidgetAlignment.Center
            };
            panel1.Children.Add(SmallLabelWidget);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, 20f) });
            StackPanelWidget obj5 = new() {
                Direction = LayoutDirection.Horizontal, Margin = new Vector2(0f, 5f), HorizontalAlignment = WidgetAlignment.Center
            };
            Button1Widget = new BevelledButtonWidget { Size = new Vector2(200f, 60f), Margin = new Vector2(20f, 0f), Sound = "RCSounds/Click2" };
            obj5.Children.Add(Button1Widget);
            Button2Widget = new BevelledButtonWidget { Size = new Vector2(200f, 60f), Margin = new Vector2(20f, 0f), Sound = "RCSounds/Click2" };
            obj5.Children.Add(Button2Widget);
            panel1.Children.Add(obj5);
            obj[2] = panel1;
            Children.Add(obj);
            switch (player.Status) {
                case FactionStatus.WonEliminatedOthers:
                    LargeLabelWidget.Text = LanguageControl.Get(fName, "1");
                    SmallLabelWidget.Text = LanguageControl.Get(fName, "2");
                    Button1Widget.Text = LanguageControl.Get(fName, "3");
                    Button2Widget.IsVisible = false;
                    AudioManager.PlaySound(Sounds.Victory, false);
                    break;
                case FactionStatus.Won:
                    LargeLabelWidget.Text = LanguageControl.Get(fName, "1");
                    SmallLabelWidget.Text = LanguageControl.Get(fName, "4");
                    Button1Widget.Text = LanguageControl.Get(fName, "5");
                    Button2Widget.IsVisible = false;
                    AudioManager.PlaySound(Sounds.Victory, false);
                    break;
                case FactionStatus.Lost: {
                    LargeLabelWidget.Text = LanguageControl.Get(fName, "6");
                    Player player2 = player.Game.PlayersModule.Players.FirstOrDefault(p => p.Status == FactionStatus.Won);
                    if (player2 != null) {
                        SmallLabelWidget.Text = string.Format(LanguageControl.Get(fName, "7"), player2.Name);
                        SmallLabelWidget.Color = Player.GetColor(player2.Faction);
                    }
                    Button1Widget.Text = LanguageControl.Get(fName, "8");
                    Button2Widget.IsVisible = false;
                    AudioManager.PlaySound(Sounds.Defeat, false);
                    break;
                }
                case FactionStatus.LostEliminated:
                    LargeLabelWidget.Text = LanguageControl.Get(fName, "6");
                    SmallLabelWidget.Text = LanguageControl.Get(fName, "9");
                    Button1Widget.Text = LanguageControl.Get(fName, "10");
                    Button2Widget.Text = LanguageControl.Get(fName, "11");
                    AudioManager.PlaySound(Sounds.Defeat, false);
                    break;
            }
        }

        public override void Update() {
            if (Input.Cancel) {
                if (Button2Widget.IsVisible) {
                    Dismiss(MessageDialogButton.Button2);
                }
                else {
                    Dismiss(MessageDialogButton.Button1);
                }
            }
            else if (Input.Ok
                || Button1Widget.IsClicked) {
                Dismiss(MessageDialogButton.Button1);
            }
            else if (Button2Widget.IsClicked) {
                Dismiss(MessageDialogButton.Button2);
            }
        }

        public void Dismiss(MessageDialogButton button) {
            DialogsManager.HideDialog(this);
            if (button == MessageDialogButton.Button1) {
                ScreensManager.SwitchScreen("RCMainMenu");
            }
        }
    }
}